package character;

import config.Config;
import sprite.Animator;
import sprite.Transform;
import utils.Vector2d;
import world.Background;

import java.awt.*;
import java.util.Random;

public class Enemy extends ActorObject{
    private Animator _animator;
    private Player _player;
    private double _speed = 1.5;
    private boolean _dead = false;

    public Enemy(Player players, Animator animator, int hurtx, int hurty, int hurtscale_x, int hurtscale_y
            , int hitoffset_x1, int hitoffset_y1, int hitoffset_x2, int hitoffset_y2){
        super(animator,100,1, hurtx,hurty,hurtscale_x,hurtscale_y
                ,hitoffset_x1,hitoffset_y1,hitoffset_x2,hitoffset_y2);
        _animator = animator;
        _player= players;
        _animator.setAnimation("idle");
    }


    public void move(double x, double y){
        if(!_dead) {
            if (!isBlocked()) {
                setZ(getZ() + y);
                getTransform().translate(x, y);
                if (!getAnimator().isPlaying("walk")) {
                    getAnimator().setAnimation("walk");
                }
            }
        }
    }

//    @Override
//    public void update(Graphics2D g){
//        if(!this.isDead()){
//            _animator.show(g);
//            followPath();
//            if(this.isHit(_player)){
//                this.takeDamage(_player);
//            }
//        }
//    }

    @Override
    public void update(Graphics2D g){
        if(!_dead) {
            super.update(g);
        }
        if(!isDead()){
            if(isBlocked()){
//                System.out.println("isBlocked()");
                getAnimator().setAnimation("block");
            }
            else
            {
                if(!getAnimator().isPlaying("idle")
                        && !getAnimator().isPlaying("block"))
                {
                    getAnimator().setAnimation("idle");
                }
            }
            followPath();
            if(this.isHit(_player)){
                this.takeDamage(_player);
            }
        }
        else{
            if(!_dead){
                System.out.println("die");
                getAnimator().setAnimation("die");
                _dead = true;
            }
            if(getAnimator().getMaxFrame()==getAnimator().getCurrentFrame()){
//                System.out.println("dead");
                getAnimator().setAnimation("dead");
            }
        }
    }

    public Transform getTransform(){
        return _animator;
    }

    private boolean _isInRange(){
        boolean bRet = Math.abs(getX() - _player.getX()) < 60
                && Math.abs(getZ() - _player.getZ()) < Config.Z_FIGHT_DIST;

        return bRet;
    }


    public void followPath(){
        if(!_isInRange()){

            Vector2d towards = new Vector2d(_player.getX()-getX(),
                    _player.getZ()-getZ());

            Vector2d normalizedTowards = towards.normalize();

            Vector2d posVector = new Vector2d(normalizedTowards.dX,normalizedTowards.dY);


            posVector.scale(_speed);

            posVector.add(new Vector2d(getX(),getZ()));


            setPosition(posVector.dX,posVector.dY);

            if((getX()-_player.getX())<0 && !getTransform().isFlippedRight()){
                getTransform().flip();
            }

            if((getX()-_player.getX())>=0 && getTransform().isFlippedRight()){
                getTransform().flip();
            }

        }
    }

    @Override
    void handleAttack() {

    }

    @Override
    void attack() {

    }

    @Override
    void idle() {

    }
}
